package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

/**
 * 速度数据
 */
public class SpeedDataBean {

    public Long time = 0L;
    public int power = 0; //电量
    public int signal = 0; //信号
    public int typeVel = 96; // 96 单轮无线速度传感器 _ 98 双轮无线速度传感器
    public String typeStr = "单轮" ; // 96 单轮无线速度传感器 _ 98 双轮无线速度传感器
//    private final float con = 0.01021f;
    public Float[] speedArry = new Float[10]; //速度数据
    public float speedVel = 0f; //平均速度
    public int index = -1;
    public int type = -1;
    public ComBean comBean;

    public SpeedDataBean() {
    }

    public SpeedDataBean(ComBean comBean ) {
        caculate(comBean.recData);
        time = comBean.recTime;
    }

    public SpeedDataBean(byte[] recData) {
        caculate(recData);
    }

    public SpeedDataBean caculate(ComBean comBean){

        time = comBean.recTime;
        this.comBean = comBean;
        return caculate(comBean.recData);
    }

    public SpeedDataBean caculate(byte[] buffer) {
        time = System.currentTimeMillis();
        signal = MyFunc.getSignal(buffer[36]); //测试信号
        power = MyFunc.getPower(buffer[34], buffer[35]);
        typeVel = buffer[9];
        type = buffer[9];
        index = buffer[10];
        typeStr =  typeVel == 96 ? "单轮" : "双轮";
        speedArry[0] = (float) MyFunc.speed(buffer[14], buffer[15]) ;
        speedArry[1] = (float) MyFunc.speed(buffer[16], buffer[17]) ;
        speedArry[2] = (float) MyFunc.speed(buffer[18], buffer[19]) ;
        speedArry[3] = (float) MyFunc.speed(buffer[20], buffer[21]) ;
        speedArry[4] = (float) MyFunc.speed(buffer[22], buffer[23]) ;
        speedArry[5] = (float) MyFunc.speed(buffer[24], buffer[25]) ;
        speedArry[6] = (float) MyFunc.speed(buffer[26], buffer[27]) ;
        speedArry[7] = (float) MyFunc.speed(buffer[28], buffer[29]) ;
        speedArry[8] = (float) MyFunc.speed(buffer[30], buffer[31]) ;
        speedArry[9] = (float) MyFunc.speed(buffer[32], buffer[33]) ;

        speedVel = 0f;
        for (int i = 0; i < 10; i++) {
            speedVel +=speedArry[i];
        }
        speedVel = speedVel/10f;
        return this;
    }
}
